package com.whsxt.buss.service.impl;

import com.whsxt.buss.domain.BusGoods;
import com.whsxt.buss.domain.BusInport;
import com.whsxt.buss.domain.BusOutport;
import com.whsxt.buss.mapper.BusGoodsMapper;
import com.whsxt.buss.mapper.BusInportMapper;
import com.whsxt.buss.mapper.BusOutportMapper;
import com.whsxt.buss.service.IBusInportService;
import com.whsxt.common.util.WebContextUtil;
import com.whsxt.sys.domain.User;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import java.util.Date;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author 贺炯
 * @since 2019-09-28
 */
@Service
@Transactional
public class BusInportServiceImpl extends ServiceImpl<BusInportMapper, BusInport> implements IBusInportService {
	
	@Autowired
	private BusGoodsMapper busGoodsMapper;
	
	@Autowired
	private BusOutportMapper busOutportMapper;
	
	
	
	@Override
	public void addInport(BusInport inport) {
		Integer goodsid = inport.getGoodsid();
		Integer number = inport.getNumber();
		BusGoods goods = busGoodsMapper.selectById(goodsid);
		Integer number2 = goods.getNumber();
		goods.setNumber(number2+number);
		busGoodsMapper.updateById(goods);
		User user = (User) WebContextUtil.getSession().getAttribute("currUser");
		inport.setOperateperson(user.getName());
		inport.setInporttime(new Date());
		this.getBaseMapper().insert(inport);
	}

	@Override
	public void updateInport(BusInport inport) {
		Integer id = inport.getId();
		BusInport selectById = this.getBaseMapper().selectById(id);
		//之前进货的数量
		Integer number = selectById.getNumber();
		Integer goodsid = inport.getGoodsid();
		BusGoods goods = busGoodsMapper.selectById(goodsid);
		//库存数量
		Integer number2 = goods.getNumber();
		goods.setNumber(number2-number+inport.getNumber());
		busGoodsMapper.updateById(goods);
		User user = (User) WebContextUtil.getSession().getAttribute("currUser");
		inport.setOperateperson(user.getName());
		inport.setInporttime(new Date());
		this.getBaseMapper().updateById(inport);
	}

	@Override
	public void deleteOne(Integer id) {
		BusInport inport = this.getBaseMapper().selectById(id);
		//删除进货的数量
		Integer number = inport.getNumber();
		Integer goodsid = inport.getGoodsid();
		BusGoods goods = busGoodsMapper.selectById(goodsid);
		//库存数量
		Integer number2 = goods.getNumber();
		goods.setNumber(number2-number);
		busGoodsMapper.updateById(goods);
		this.getBaseMapper().deleteById(id);
	}

	@Override
	public void back(Integer id, Integer number, String remark) {
		BusInport inport = this.getBaseMapper().selectById(id);
		Integer goodsid = inport.getGoodsid();
		BusGoods goods = busGoodsMapper.selectById(goodsid);
		Integer number2 = goods.getNumber();
		goods.setNumber(number2-number);
		busGoodsMapper.updateById(goods);
		BusOutport outport = new BusOutport();
		outport.setGoodsid(goodsid);
		outport.setNumber(number);
		outport.setRemark(remark);
		User user = (User) WebContextUtil.getSession().getAttribute("currUser");
		outport.setOperateperson(user.getName());
		outport.setOutportprice(inport.getInportprice());
		outport.setOutputtime(new Date());
		outport.setPaytype(inport.getPaytype());
		outport.setProviderid(inport.getProviderid());
		busOutportMapper.insert(outport);
	} 
}
